/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using WinForms.Renderer.GDI.Drawers;
using Core.Model.Ingredients.Presentable.Visibles;
using Bitmap=System.Drawing.Bitmap;

namespace WinForms.Renderer.GDI
{
    public class BitmapRenderer : IMHEGRenderer, IDisposable
    {
        //private Control panel;
        //private BufferedGraphicsContext graphicManager;
        //private BufferedGraphics backBuffer;
               

        //public delegate void redrawDeleagte ( List<Core.Visible> vs );
        //public delegate void refreshDelegate ( System.Drawing.Rectangle [] rs, List<Core.Visible> vs );

        public BitmapRenderer ()
        {
            //panel = new MHEGPanel( this );
            //panel.Size = MHEGDisplayConstants.DefaultMHEGPanelSize;

            //graphicManager = new BufferedGraphicsContext();
            //graphicManager.MaximumBuffer = MHEGDisplayConstants.DefaultMHEGPanelSize;

            //SetRenderingQuality();

            CreateDrawingPool();

        }


    #region IMHEGRenderer Members

        //public Control Panel
        //{
        //    get { return panel; }
        //}

        private Dictionary<Type, IDrawer> DrawerPool = new Dictionary<Type, IDrawer>( 8 );


        private void SetRenderingQuality(Graphics g)
        {
            //backBuffer = graphicManager.Allocate( panel.CreateGraphics(), panel.DisplayRectangle );
            g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
            g.PageUnit =  GraphicsUnit.Pixel;
            g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
        }

        private void CreateDrawingPool()
        {
            //Type.GetType( "GUI.Renderer.GDI.Drawers.Drawer" ).N;

            Drawer drawer = new Drawer();
            DrawerPool.Add( drawer.GetVisible, drawer );

            drawer = new AudioDrawer();
            DrawerPool.Add( drawer.GetVisible, drawer );
            
            drawer = new BitmapDrawer();
            DrawerPool.Add( drawer.GetVisible, drawer );
            
            drawer = new DynamicLineArtDrawer();
            DrawerPool.Add( drawer.GetVisible, drawer );
            
            drawer = new LineArtDrawer();
            DrawerPool.Add( drawer.GetVisible, drawer );
            
            drawer = new RectangleDrawer();
            DrawerPool.Add( drawer.GetVisible, drawer );
            
            drawer = new VideoDrawer();
            DrawerPool.Add( drawer.GetVisible, drawer );

            drawer = new TextDrawer();
            DrawerPool.Add( drawer.GetVisible, drawer );

        }


        public void Redraw ( List<Visible> vs)
        {
            //Redraw( backBuffer.Graphics , vs);
            //Repaint();
        }

        public void Refresh ( System.Drawing.Rectangle [] rs , List<Visible> vs)
        {
        //    if ( panel.InvokeRequired )
        //    {
        //        panel.BeginInvoke( new refreshDelegate( this.Refresh ), new object [] { rs, vs } );
        //        return;
        //    }
            
        //    Redraw(vs);
        }

        public Bitmap DRefresh ( System.Drawing.Rectangle [] rs, List<Visible> vs , Bitmap bitmap)
        {

            Graphics g = Graphics.FromImage( bitmap );
            SetRenderingQuality( g );
            Redraw( g, vs );

            return bitmap;

            //return GenerateAlphaBitmap();
        }


        //public void Repaint ()
        //{
        //   // try
        //   //{
               
        //        backBuffer.Render();
        //   // }
        //   // catch ( Exception e )
        //   // {
        //   // }
        //}

        public void Resize ( Size newSize )
        {
        //    this.panel.Size = newSize;
        //    backBuffer.Graphics.ResetTransform();

        //    //float xscale = (float) newSize.Width / MHEGDisplayConstants.DefaultMHEGPanelSize.Width;
        //    //float yscale = (float) newSize.Height / MHEGDisplayConstants.DefaultMHEGPanelSize.Height;
        //    //backBuffer.Graphics.ScaleTransform ( xscale, yscale);

        //    Repaint();

        }


        #endregion

        internal void Redraw ( Graphics graphics, List<Visible> vs )
        {

            //graphics.Clear( Core.Engine.MHEGConstants.DesktopColour.Value );
            graphics.Clear( Color.Green );
            
            foreach ( Visible v in vs )
            {
                graphics.Clip = new Region(new System.Drawing.Rectangle(v.BoxPosition , v.BoxSize));
                DrawerPool [v.GetType()].Draw( v, graphics );
                
            }

            graphics.Clip.MakeInfinite();
        }

        public void Clear (Bitmap bitmap)
        {
            //backBuffer.Graphics.Clear( Color.Black );
            //Repaint();

            Graphics g = Graphics.FromImage( bitmap );
            g.Clear(Color.Green);
            
        }

        #region IDisposable Members

        public void Dispose ()
        {
            //graphicManager.Dispose();
            //backBuffer.Dispose();

            //bitmap.Dispose();
            
        }

        #endregion




        #region IMHEGRenderer Members


        public void Clear ()
        {
            throw new Exception( "The method or operation is not implemented." );
        }

        #endregion
    }
}
